package cn.dengta.webapp.im.entity;

import java.util.Date;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.model.ValueProxyImpl;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.model.Gender;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 站内信配置表
 *
 * @author lgy
 * @since 2022-07-11
 */
@Setter
@Getter
@Accessors(chain = true)
public class SystemMsg extends BizEntity {

	/** 用户ID */
	private Long userId;
	/** 类型 @see SystemMsgType */
	private Integer type;
	/** 用户ID（多个/n隔开） */
	private String userIds;
	/** 推广员ID（多个/n隔开） */
	private String agentIds;
	/** 性别：1=男；0=女 */
	private Gender gender;
	/** 语言 */
	private Language language;
	/** 分包（多个,隔开） */
	private String wesKit;
	/** 发送数 */
	private Integer sendCount;
	/** 内容，为图片时内容为图片链接 */
	private String content;
	/** 消息类型：0=文本；1=图片 */
	private Integer msgType;
	/** 发送时间 */
	private Date sendTime;

	public SystemMsg init(SystemMsg input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.type = input.type;
		this.userIds = StringUtil.toString(input.userIds);
		this.agentIds = StringUtil.toString(input.agentIds);
		this.gender = input.gender;
		this.language = input.language;
		this.content = StringUtil.toString(input.content);
		this.sendTime = input.sendTime;
		this.wesKit = input.wesKit;
		this.msgType = input.msgType;
		super.init(input.status, input.state);
		super.initTime(now);
		return this;
	}

	public boolean canEdit() {
		return SystemMsgStatus.WAIT.eq(status);
	}

	public boolean canSend() {
		return SystemMsgStatus.WAIT.eq(status) || SystemMsgStatus.PROCESSING.eq(status);
	}

	@Getter
	public enum SystemMsgStatus implements ValueProxyImpl<SystemMsgStatus, Integer> {
		/** 删除 */
		CANCEL(-99, "删除"),
		/** 待处理 */
		WAIT(0, "待处理"),
		/** 处理中 */
		PROCESSING(1, "处理中"),
		/** 已发放 */
		ISSUED(2, "已发放"),
		//
		;

		public final Integer value;
		public final ValueProxy<SystemMsgStatus, Integer> proxy;

		SystemMsgStatus(Integer value, String label) {
			this.value = value;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		public static SystemMsgStatus[] getValues() {
			return ValueProxy.getCachedAll(SystemMsgStatus.class);
		}

		public static SystemMsgStatus of(Integer value) {
			return WAIT.getValueOf(value);
		}

	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public static final String USER_ID = "user_id";
	public static final String TYPE = "type";
	public static final String USER_IDS = "user_ids";
	public static final String AGENT_IDS = "agent_ids";
	public static final String GENDER = "gender";
	public static final String LANGUAGE = "language";
	public static final String CONTENT = "content";
	public static final String SEND_TIME = "send_time";

}
